﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RaGE.Properties;

namespace RaGE
{
    public partial class MainWindow
    {
        /*
        private void JDNPCInitialize()
        {
            clientOffsets = new ArrayList();

            if (File.Exists(Application.StartupPath + "\\jdoffsets.txt"))
            {
                try
                {
                    var sr = new StreamReader(Application.StartupPath + "\\jdoffsets.txt", Encoding.Unicode);

                    var seperator = new[] { ' ' };
                    string line;

                    while (!string.IsNullOrEmpty((line = sr.ReadLine())))
                    {
                        if (!line.StartsWith("Name=")) continue;

                        comboBox_AssistantClient.Items.Add(line.Substring(5));

                        var offset = new JDOffset();

                        // target base offset chain
                        var readLine = sr.ReadLine();

                        if (readLine != null)
                        {
                            var values = readLine.Split(seperator);
                            offset.BaseChain = new int[values.Length];

                            for (var i = 0; i < values.Length; i++)
                                offset.BaseChain[i] = Int32.Parse(values[i], NumberStyles.HexNumber);
                        }

                        offset.DirX = Int32.Parse(sr.ReadLine(), NumberStyles.HexNumber);
                        offset.DirY = Int32.Parse(sr.ReadLine(), NumberStyles.HexNumber);
                        offset.DirZ = Int32.Parse(sr.ReadLine(), NumberStyles.HexNumber);
                        offset.PosX = Int32.Parse(sr.ReadLine(), NumberStyles.HexNumber);
                        offset.PosY = Int32.Parse(sr.ReadLine(), NumberStyles.HexNumber);
                        offset.PosZ = Int32.Parse(sr.ReadLine(), NumberStyles.HexNumber);

                        clientOffsets.Add(offset);
                    }

                    sr.Close();

                    if (comboBox_AssistantClient.Items.Count > 0)
                        comboBox_AssistantClient.SelectedIndex = 0;

                }
// ReSharper disable EmptyGeneralCatchClause
                catch (Exception)
// ReSharper restore EmptyGeneralCatchClause
                {
                }
            }

            JDnpcDB = new SortedList();

            if (!File.Exists(Application.StartupPath + "\\jdnpcdb.txt")) return;

            try
            {
                var sr = new StreamReader(Application.StartupPath + "\\jdnpcdb.txt", Encoding.Unicode);

                var seperator = new[] { '\t' };
                string line;

                while (!string.IsNullOrEmpty((line = sr.ReadLine())))
                {
                    if (!line.Contains("\t")) continue;

                    var split = line.Split(seperator);
                    JDnpcDB.Add(split[0], split[1]);
                }

                sr.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(Resources.ErrLoadNPCDB + e.Message);
            }
        }
        */
        private void JDInitilaizeResDB()
        {
            JDresDB = new SortedList();

            if (!File.Exists(Application.StartupPath + "\\jdrestoname.txt")) return;

            try
            {
                var streamReader = new StreamReader(Application.StartupPath + "\\jdrestoname.txt", Encoding.Unicode);
                var separator = new[] { '\t' };

                while (true)
                {
                    var text = streamReader.ReadLine();

                    if (text == null)
                        break;

                    if (!text.Contains("\t")) continue;

                    var array = text.Split(separator);
                    JDresDB.Add(array[0], array[1]);
                }
                streamReader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Resources.ErrReadResTbl + ex.Message);
            }
        }

        private void JDInitilaizeNPCDB()
        {
            JDnpcDB = new SortedList();
            JDnpcID = new List<SortedList>();
            JDnpcID.Add(new SortedList());

            if (!File.Exists(Application.StartupPath + "\\jdidtoname.txt")) return;

            try
            {
                var streamReader = new StreamReader(Application.StartupPath + "\\jdidtoname.txt", Encoding.Unicode);
                var separator = new[] { '\t' };

                while (streamReader.Peek() >= 0)
                {
                    var text = streamReader.ReadLine();

                    if (text != null && !text.Contains("\t")) continue;

                    if (text == null) continue;

                    var array = text.Split(separator);
                    JDnpcDB.Add(array[0], array[1]);

                    for (var i = 0; i < JDnpcID.Count; i++)
                    {
                        if (!JDnpcID[i].ContainsKey(array[1]))
                        {
                            JDnpcID[i].Add(array[1], array[0]);
                            break;
                        }
                        if (i != JDnpcID.Count - 1) continue;

                        JDnpcID.Add(new SortedList());
                        JDnpcID[i + 1].Add(array[1], array[0]);
                        break;
                    }
                }
                streamReader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Resources.ErrReadDataTbl + ex.Message);
            }
        }

        private void JDNPCInitialize()
        {
            JDInitilaizeResDB();
            JDInitilaizeNPCDB();
            var initPath = Application.StartupPath + "\\License.sys";

            if (File.Exists(initPath))
            {
                var fileStream = File.OpenRead(initPath);
                var num = (int)fileStream.Length;
                var array = new byte[num];
                var encoding = Encoding.GetEncoding("GB2312");

                fileStream.Read(array, 0, array.Length);

                var @string = encoding.GetString(array);
                richTextBox1.Text = @string;
                fileStream.Close();
            }

            getXYZ = new GetXYZ();

            foreach (var offset in getXYZ.clientOffsets.Cast<JDOffset>())
                comboBox_AssistantClient.Items.Add(offset.Version);

            if (comboBox_AssistantClient.Items.Count > 0)
                comboBox_AssistantClient.SelectedIndex = 0;
        }
        
        private string JDNPCName(int id)
        {
            var result = (string)JDnpcDB[id.ToString(CultureInfo.InvariantCulture)];
            return result ?? ("Not found...");
        }
        
        private void JDDrawCreatures(IList<JDCreatureSets> cs)
        {
            dataGridView_JDCreatureGroups.Rows.Clear();
            dataGridView_JDCreatures.Rows.Clear();

            for (var i = 0; i < cs.Count; i++)
            {
                var array = new string[22];
                var creatureGroupsCount = cs[i].creature_groups_count;

                array[0] = creatureGroupsCount.ToString(CultureInfo.InvariantCulture);
                array[1] = Column1.Items[cs[i].spawn_mode].ToString();

                var spawnX = cs[i].spawn_x;
                array[2] = spawnX.ToString("F3");

                var spawnAlt = cs[i].spawn_alt;
                array[3] = spawnAlt.ToString("F3");

                var spawnZ = cs[i].spawn_z;
                array[4] = spawnZ.ToString("F3");

                var rot = cs[i].rot_1;
                array[5] = rot.ToString("F3");

                var rot2 = cs[i].rot_2;
                array[6] = rot2.ToString("F3");

                var rot3 = cs[i].rot_3;
                array[7] = rot3.ToString("F3");

                var spreadX = cs[i].spread_x;
                array[8] = spreadX.ToString("F3");

                var spreadAlt = cs[i].spread_alt;
                array[9] = spreadAlt.ToString("F3");

                var spreadZ = cs[i].spread_z;
                array[10] = spreadZ.ToString("F3");
                array[11] = Column12.Items[cs[i].unknown_7].ToString();

                var unknown = cs[i].unknown_8;
                array[12] = unknown.ToString(CultureInfo.InvariantCulture);

                var unknown2 = cs[i].unknown_9;
                array[13] = unknown2.ToString();

                var unknown3 = cs[i].unknown_10;
                array[14] = unknown3.ToString();

                var unknown4 = cs[i].unknown_11;
                array[15] = unknown4.ToString();

                var unknown5 = cs[i].unknown_12;
                array[16] = unknown5.ToString(CultureInfo.InvariantCulture);

                var trigger = cs[i].trigger;
                array[17] = trigger.ToString(CultureInfo.InvariantCulture);

                var unknown6 = cs[i].unknown_14;
                array[18] = unknown6.ToString(CultureInfo.InvariantCulture);

                var unknown7 = cs[i].unknown_15;
                array[19] = unknown7.ToString(CultureInfo.InvariantCulture);

                var unknown8 = cs[i].unknown_16;
                array[20] = unknown8.ToString(CultureInfo.InvariantCulture);

                var unknown9 = cs[i].unknown_17;
                array[21] = unknown9.ToString(CultureInfo.InvariantCulture);

// ReSharper disable CoVariantArrayConversion
                dataGridView_JDCreatures.Rows.Add(array);
// ReSharper restore CoVariantArrayConversion

                if (cs[i].creature_groups_count <= 0) continue;

                var id = cs[i].creature_groups[0].id;

                dataGridView_JDCreatures.Rows[i].HeaderCell.Value =
                    "[" + id.ToString(CultureInfo.InvariantCulture) + "] " + JDNPCName(cs[i].creature_groups[0].id);
            }

        }

        private void JDLoadCreatures(BinaryReader br)
        {
            dataGridView_JDCreatures.Rows.Clear();

            jdnpcgen.creature_sets = new JDCreatureSets[jdnpcgen.creature_sets_count];

            for (var i = 0; i < jdnpcgen.creature_sets.Length; i++)
            {
                jdnpcgen.creature_sets[i] = new JDCreatureSets();
                jdnpcgen.creature_sets[i].spawn_mode = br.ReadInt32();
                jdnpcgen.creature_sets[i].creature_groups_count = br.ReadInt32();
                jdnpcgen.creature_sets[i].spawn_x = br.ReadSingle();
                jdnpcgen.creature_sets[i].spawn_alt = br.ReadSingle();
                jdnpcgen.creature_sets[i].spawn_z = br.ReadSingle();
                jdnpcgen.creature_sets[i].rot_1 = br.ReadSingle();
                jdnpcgen.creature_sets[i].rot_2 = br.ReadSingle();
                jdnpcgen.creature_sets[i].rot_3 = br.ReadSingle();
                jdnpcgen.creature_sets[i].spread_x = br.ReadSingle();
                jdnpcgen.creature_sets[i].spread_alt = br.ReadSingle();
                jdnpcgen.creature_sets[i].spread_z = br.ReadSingle();
                jdnpcgen.creature_sets[i].unknown_7 = br.ReadInt32();
                jdnpcgen.creature_sets[i].unknown_8 = br.ReadInt32();
                jdnpcgen.creature_sets[i].unknown_9 = br.ReadBoolean();
                jdnpcgen.creature_sets[i].unknown_10 = br.ReadBoolean();
                jdnpcgen.creature_sets[i].unknown_11 = br.ReadBoolean();
                jdnpcgen.creature_sets[i].unknown_12 = br.ReadInt32();
                jdnpcgen.creature_sets[i].trigger = br.ReadInt32();
                jdnpcgen.creature_sets[i].unknown_14 = br.ReadInt32();
                jdnpcgen.creature_sets[i].unknown_15 = br.ReadInt32();
                jdnpcgen.creature_sets[i].unknown_16 = br.ReadInt32();
                jdnpcgen.creature_sets[i].unknown_17 = br.ReadInt32();

                jdnpcgen.creature_sets[i].creature_groups = new JDCreatureGroup[jdnpcgen.creature_sets[i].creature_groups_count];

                for (var j = 0; j < jdnpcgen.creature_sets[i].creature_groups.Length; j++)
                {
                    jdnpcgen.creature_sets[i].creature_groups[j] = new JDCreatureGroup();

                    jdnpcgen.creature_sets[i].creature_groups[j].id = br.ReadInt32();
                    jdnpcgen.creature_sets[i].creature_groups[j].amount = br.ReadInt32();
                    jdnpcgen.creature_sets[i].creature_groups[j].respawn = br.ReadInt32();
                    jdnpcgen.creature_sets[i].creature_groups[j].unknown_2 = br.ReadInt32();
                    jdnpcgen.creature_sets[i].creature_groups[j].unknown_3 = br.ReadInt32();
                    jdnpcgen.creature_sets[i].creature_groups[j].unknown_4 = br.ReadInt32();
                    jdnpcgen.creature_sets[i].creature_groups[j].unknown_5 = br.ReadInt32();
                    jdnpcgen.creature_sets[i].creature_groups[j].unknown_6 = br.ReadInt32();
                    jdnpcgen.creature_sets[i].creature_groups[j].unknown_7 = br.ReadInt32();
                    jdnpcgen.creature_sets[i].creature_groups[j].unknown_8 = br.ReadInt32();
                    jdnpcgen.creature_sets[i].creature_groups[j].unknown_9 = br.ReadBoolean();
                    jdnpcgen.creature_sets[i].creature_groups[j].unknown_10 = br.ReadBoolean();
                    jdnpcgen.creature_sets[i].creature_groups[j].unknown_11 = br.ReadBoolean();
                    jdnpcgen.creature_sets[i].creature_groups[j].unknown_12 = br.ReadBoolean();
                    jdnpcgen.creature_sets[i].creature_groups[j].unknown_13 = br.ReadInt32();
                    jdnpcgen.creature_sets[i].creature_groups[j].unknown_14 = br.ReadInt32();
                    jdnpcgen.creature_sets[i].creature_groups[j].unknown_15 = br.ReadInt32();
                    jdnpcgen.creature_sets[i].creature_groups[j].unknown_16 = br.ReadInt32();
                }

                if (jdnpcgen.creature_sets[i].unknown_17 <= 0) continue;

                jdnpcgen.creature_sets[i].mutualExclusionGroup = new int[jdnpcgen.creature_sets[i].unknown_17];

                for (var k = 0; k < jdnpcgen.creature_sets[i].unknown_17; k++)
                    jdnpcgen.creature_sets[i].mutualExclusionGroup[k] = br.ReadInt32();
            }
            JDDrawCreatures(jdnpcgen.creature_sets);
        }

        private string JDResName(int id)
        {
            var num = id;
            var text = (string)JDresDB[num.ToString(CultureInfo.InvariantCulture)] ?? "Not found...";
            return text;
        }
        
        private void JDDrawResources(IList<JDResourceSets> cs)
        {
            dataGridView_JDResources.Rows.Clear();

            for (var i = 0; i < cs.Count; i++)
            {
                var array = new string[17];
                var resourceGroupsCount = cs[i].resource_groups_count;
                array[0] = resourceGroupsCount.ToString(CultureInfo.InvariantCulture);

                var spawnX = cs[i].spawn_x;
                array[1] = spawnX.ToString("F3");

                var spawnAlt = cs[i].spawn_alt;
                array[2] = spawnAlt.ToString("F3");

                var spawnZ = cs[i].spawn_z;
                array[3] = spawnZ.ToString("F3");

                var spreadX = cs[i].spread_x;
                array[4] = spreadX.ToString("F3");

                var spreadZ = cs[i].spread_z;
                array[5] = spreadZ.ToString("F3");

                var unknown = cs[i].unknown_1;
                array[6] = unknown.ToString();

                var unknown2 = cs[i].unknown_2;
                array[7] = unknown2.ToString();

                var unknown3 = cs[i].unknown_3;
                array[8] = unknown3.ToString();

                var unknown4 = cs[i].unknown_4;
                array[9] = unknown4.ToString(CultureInfo.InvariantCulture);

                var unknown5 = cs[i].unknown_5;
                array[10] = unknown5.ToString(CultureInfo.InvariantCulture);

                var unknown6 = cs[i].unknown_6;
                array[11] = unknown6.ToString(CultureInfo.InvariantCulture);

                var unknown7 = cs[i].unknown_7;
                array[12] = unknown7.ToString(CultureInfo.InvariantCulture);

                var unknownTrigger = cs[i].unknown_trigger;
                array[13] = unknownTrigger.ToString(CultureInfo.InvariantCulture);

                var unknown8 = cs[i].unknown_8;
                array[14] = unknown8.ToString(CultureInfo.InvariantCulture);

                var unknownIndex = cs[i].unknown_index;
                array[15] = unknownIndex.ToString(CultureInfo.InvariantCulture);

                var unknownMutual = cs[i].unknown_mutual;
                array[16] = unknownMutual.ToString(CultureInfo.InvariantCulture);

// ReSharper disable CoVariantArrayConversion
                dataGridView_JDResources.Rows.Add(array);
// ReSharper restore CoVariantArrayConversion

                if (cs[i].resource_groups_count <= 0) continue;

                var id = cs[i].resource_groups[0].id;
                dataGridView_JDResources.Rows[i].HeaderCell.Value =
                    "[" + id.ToString(CultureInfo.InvariantCulture) + "] " + JDResName(cs[i].resource_groups[0].id);
            }
        }

        private void JDLoadResources(BinaryReader br)
        {
            jdnpcgen.resource_sets = new JDResourceSets[jdnpcgen.resource_sets_count];

            for (var i = 0; i < jdnpcgen.resource_sets.Length; i++)
            {
                jdnpcgen.resource_sets[i] = new JDResourceSets();

                jdnpcgen.resource_sets[i].spawn_x = br.ReadSingle();
                jdnpcgen.resource_sets[i].spawn_alt = br.ReadSingle();
                jdnpcgen.resource_sets[i].spawn_z = br.ReadSingle();
                jdnpcgen.resource_sets[i].spread_x = br.ReadSingle();
                jdnpcgen.resource_sets[i].spread_z = br.ReadSingle();
                jdnpcgen.resource_sets[i].resource_groups_count = br.ReadInt32();
                jdnpcgen.resource_sets[i].unknown_1 = br.ReadBoolean();
                jdnpcgen.resource_sets[i].unknown_2 = br.ReadBoolean();
                jdnpcgen.resource_sets[i].unknown_3 = br.ReadBoolean();
                jdnpcgen.resource_sets[i].unknown_4 = br.ReadInt32();
                jdnpcgen.resource_sets[i].unknown_5 = br.ReadByte();
                jdnpcgen.resource_sets[i].unknown_6 = br.ReadByte();
                jdnpcgen.resource_sets[i].unknown_7 = br.ReadByte();
                jdnpcgen.resource_sets[i].unknown_trigger = br.ReadInt32();
                jdnpcgen.resource_sets[i].unknown_8 = br.ReadInt32();
                jdnpcgen.resource_sets[i].unknown_index = br.ReadInt32();
                jdnpcgen.resource_sets[i].unknown_mutual = br.ReadInt32();

                jdnpcgen.resource_sets[i].resource_groups = new JDResourceGroup[jdnpcgen.resource_sets[i].resource_groups_count];

                for (var j = 0; j < jdnpcgen.resource_sets[i].resource_groups.Length; j++)
                {
                    jdnpcgen.resource_sets[i].resource_groups[j] = new JDResourceGroup();

                    jdnpcgen.resource_sets[i].resource_groups[j].unknown_1 = br.ReadInt32();
                    jdnpcgen.resource_sets[i].resource_groups[j].id = br.ReadInt32();
                    jdnpcgen.resource_sets[i].resource_groups[j].respawn = br.ReadInt32();
                    jdnpcgen.resource_sets[i].resource_groups[j].amount = br.ReadInt32();
                    jdnpcgen.resource_sets[i].resource_groups[j].unknown_2 = br.ReadInt32();
                }

                if (jdnpcgen.resource_sets[i].unknown_mutual <= 0) continue;

                jdnpcgen.resource_sets[i].mutualExclusionGroup = new int[jdnpcgen.resource_sets[i].unknown_mutual];

                for (var k = 0; k < jdnpcgen.resource_sets[i].unknown_mutual; k++)
                    jdnpcgen.resource_sets[i].mutualExclusionGroup[k] = br.ReadInt32();
            }
            JDDrawResources(jdnpcgen.resource_sets);
        }

        private void JDDrawDynamics(IList<JDDynamic> cs)
        {
            dataGridView_JDDynamics.Rows.Clear();

            for (var i = 0; i < cs.Count; i++)
            {
                var array = new string[9];
                var id = cs[i].id;
                array[0] = id.ToString(CultureInfo.InvariantCulture);

                var spawnX = cs[i].spawn_x;
                array[1] = spawnX.ToString("F3");

                var spawnAlt = cs[i].spawn_alt;
                array[2] = spawnAlt.ToString("F3");

                var spawnZ = cs[i].spawn_z;
                array[3] = spawnZ.ToString("F3");

                var unknown = cs[i].unknown_5;
                array[4] = unknown.ToString(CultureInfo.InvariantCulture);

                var unknown2 = cs[i].unknown_6;
                array[5] = unknown2.ToString(CultureInfo.InvariantCulture);

                var unknown3 = cs[i].unknown_7;
                array[6] = unknown3.ToString(CultureInfo.InvariantCulture);

                var unknown4 = cs[i].unknown_8;
                array[7] = unknown4.ToString(CultureInfo.InvariantCulture);

                var trigger = cs[i].trigger;
                array[8] = trigger.ToString(CultureInfo.InvariantCulture);

// ReSharper disable CoVariantArrayConversion
                dataGridView_JDDynamics.Rows.Add(array);
// ReSharper restore CoVariantArrayConversion

                var num = dataGridView_JDDynamics.Rows.Count - 1;
                dataGridView_JDDynamics.Rows[i].HeaderCell.Value = num.ToString(CultureInfo.InvariantCulture);
            }
        }
        
        private void JDLoadDynamics(BinaryReader br)
        {
            dataGridView_JDDynamics.Rows.Clear();
            jdnpcgen.dynamics = new JDDynamic[jdnpcgen.dynamics_count];

            for (var i = 0; i < jdnpcgen.dynamics.Length; i++)
            {
                jdnpcgen.dynamics[i] = new JDDynamic();
                jdnpcgen.dynamics[i].id = br.ReadInt32();
                jdnpcgen.dynamics[i].spawn_x = br.ReadSingle();
                jdnpcgen.dynamics[i].spawn_alt = br.ReadSingle();
                jdnpcgen.dynamics[i].spawn_z = br.ReadSingle();
                jdnpcgen.dynamics[i].unknown_5 = br.ReadByte();
                jdnpcgen.dynamics[i].unknown_6 = br.ReadByte();
                jdnpcgen.dynamics[i].unknown_7 = br.ReadByte();
                jdnpcgen.dynamics[i].unknown_8 = br.ReadByte();
                jdnpcgen.dynamics[i].trigger = br.ReadInt32();
            }
            JDDrawDynamics(jdnpcgen.dynamics);
        }

        private void JDDrawTriggers(IList<JDTrigger> cs)
        {
            dataGridView_JDTriggers.Rows.Clear();
            var encoding = Encoding.GetEncoding("GBK");

            for (var i = 0; i < cs.Count; i++)
            {
                var array = new string[23];

                var unknown = cs[i].unknown_1;
                array[0] = unknown.ToString(CultureInfo.InvariantCulture);

                var unknown2 = cs[i].unknown_2;
                array[1] = unknown2.ToString(CultureInfo.InvariantCulture);
                array[2] = encoding.GetString(cs[i].name);

                var unknown3 = cs[i].unknown_3;
                array[3] = unknown3.ToString();

                var unknown4 = cs[i].unknown_4;
                array[4] = unknown4.ToString(CultureInfo.InvariantCulture);

                var unknown5 = cs[i].unknown_5;
                array[5] = unknown5.ToString(CultureInfo.InvariantCulture);

                var unknown6 = cs[i].unknown_6;
                array[6] = unknown6.ToString();

                var unknown7 = cs[i].unknown_7;
                array[7] = unknown7.ToString();

                var year = cs[i].year_1;
                array[8] = year.ToString(CultureInfo.InvariantCulture);

                var month = cs[i].month_1;
                array[9] = month.ToString(CultureInfo.InvariantCulture);
                array[10] = Column42.Items[cs[i].week_day_1 + 1].ToString();

                var day = cs[i].day_1;
                array[11] = day.ToString(CultureInfo.InvariantCulture);

                var hour = cs[i].hour_1;
                array[12] = hour.ToString(CultureInfo.InvariantCulture);

                var minute = cs[i].minute_1;
                array[13] = minute.ToString(CultureInfo.InvariantCulture);

                var year2 = cs[i].year_2;
                array[14] = year2.ToString(CultureInfo.InvariantCulture);

                var month2 = cs[i].month_2;
                array[15] = month2.ToString(CultureInfo.InvariantCulture);
                array[16] = Column43.Items[cs[i].week_day_2 + 1].ToString();

                var day2 = cs[i].day_2;
                array[17] = day2.ToString(CultureInfo.InvariantCulture);

                var hour2 = cs[i].hour_2;
                array[18] = hour2.ToString(CultureInfo.InvariantCulture);

                var minute2 = cs[i].minute_2;
                array[19] = minute2.ToString(CultureInfo.InvariantCulture);

                var duration = cs[i].duration;
                array[20] = duration.ToString(CultureInfo.InvariantCulture);

                var forcedStop = cs[i].forced_stop;
                array[21] = forcedStop.ToString();

                //var unknown8 = cs[i].unknown_8;
                //array[22] = unknown8.ToString();

// ReSharper disable CoVariantArrayConversion
                dataGridView_JDTriggers.Rows.Add(array);
// ReSharper restore CoVariantArrayConversion

                var num = dataGridView_JDTriggers.Rows.Count - 1;

                dataGridView_JDTriggers.Rows[i].HeaderCell.Value = num.ToString(CultureInfo.InvariantCulture);
            }
        }

        private void JDLoadTriggers(BinaryReader br)
        {
            dataGridView_JDTriggers.Rows.Clear();
            jdnpcgen.triggers = new JDTrigger[jdnpcgen.triggers_count];

            for (var i = 0; i < jdnpcgen.triggers.Length; i++)
            {
                jdnpcgen.triggers[i] = new JDTrigger();
                jdnpcgen.triggers[i].unknown_1 = br.ReadInt32();
                jdnpcgen.triggers[i].unknown_2 = br.ReadInt32();
                jdnpcgen.triggers[i].name = br.ReadBytes(128);
                jdnpcgen.triggers[i].unknown_3 = br.ReadBoolean();
                jdnpcgen.triggers[i].unknown_4 = br.ReadInt32();
                jdnpcgen.triggers[i].unknown_5 = br.ReadInt32();
                jdnpcgen.triggers[i].unknown_6 = br.ReadBoolean();
                jdnpcgen.triggers[i].unknown_7 = br.ReadBoolean();
                jdnpcgen.triggers[i].year_1 = br.ReadInt32();
                jdnpcgen.triggers[i].month_1 = br.ReadInt32();
                jdnpcgen.triggers[i].week_day_1 = br.ReadInt32();
                jdnpcgen.triggers[i].day_1 = br.ReadInt32();
                jdnpcgen.triggers[i].hour_1 = br.ReadInt32();
                jdnpcgen.triggers[i].minute_1 = br.ReadInt32();
                jdnpcgen.triggers[i].year_2 = br.ReadInt32();
                jdnpcgen.triggers[i].month_2 = br.ReadInt32();
                jdnpcgen.triggers[i].week_day_2 = br.ReadInt32();
                jdnpcgen.triggers[i].day_2 = br.ReadInt32();
                jdnpcgen.triggers[i].hour_2 = br.ReadInt32();
                jdnpcgen.triggers[i].minute_2 = br.ReadInt32();
                jdnpcgen.triggers[i].duration = br.ReadInt32();
                jdnpcgen.triggers[i].forced_stop = br.ReadBoolean();
                //jdnpcgen.triggers[i].unknown_8 = br.ReadBoolean();
            }
            JDDrawTriggers(jdnpcgen.triggers);
        }

        private void JDSaveCreatures(BinaryWriter bw)
        {
            foreach (var t in jdnpcgen.creature_sets)
            {
                bw.Write(t.spawn_mode);
                bw.Write(t.creature_groups_count);
                bw.Write(t.spawn_x);
                bw.Write(t.spawn_alt);
                bw.Write(t.spawn_z);
                bw.Write(t.rot_1);
                bw.Write(t.rot_2);
                bw.Write(t.rot_3);
                bw.Write(t.spread_x);
                bw.Write(t.spread_alt);
                bw.Write(t.spread_z);
                bw.Write(t.unknown_7);
                bw.Write(t.unknown_8);
                bw.Write(t.unknown_9);
                bw.Write(t.unknown_10);
                bw.Write(t.unknown_11);
                bw.Write(t.unknown_12);
                bw.Write(t.trigger);
                bw.Write(t.unknown_14);
                bw.Write(t.unknown_15);
                bw.Write(t.unknown_16);
                bw.Write(t.unknown_17);

                foreach (var u in t.creature_groups)
                {
                    bw.Write(u.id);
                    bw.Write(u.amount);
                    bw.Write(u.respawn);
                    bw.Write(u.unknown_2);
                    bw.Write(u.unknown_3);
                    bw.Write(u.unknown_4);
                    bw.Write(u.unknown_5);
                    bw.Write(u.unknown_6);
                    bw.Write(u.unknown_7);
                    bw.Write(u.unknown_8);
                    bw.Write(u.unknown_9);
                    bw.Write(u.unknown_10);
                    bw.Write(u.unknown_11);
                    bw.Write(u.unknown_12);
                    bw.Write(u.unknown_13);
                    bw.Write(u.unknown_14);
                    bw.Write(u.unknown_15);
                    bw.Write(u.unknown_16);
                }

                if (t.unknown_17 <= 0) continue;

                for (var k = 0; k < t.unknown_17; k++)
                    bw.Write(t.mutualExclusionGroup[k]);
            }
        }

        private void JDSaveResources(BinaryWriter bw)
        {
            foreach (var t in jdnpcgen.resource_sets)
            {
                bw.Write(t.spawn_x);
                bw.Write(t.spawn_alt);
                bw.Write(t.spawn_z);
                bw.Write(t.spread_x);
                bw.Write(t.spread_z);
                bw.Write(t.resource_groups_count);
                bw.Write(t.unknown_1);
                bw.Write(t.unknown_2);
                bw.Write(t.unknown_3);
                bw.Write(t.unknown_4);
                bw.Write(t.unknown_5);
                bw.Write(t.unknown_6);
                bw.Write(t.unknown_7);
                bw.Write(t.unknown_trigger);
                bw.Write(t.unknown_8);
                bw.Write(t.unknown_index);
                bw.Write(t.unknown_mutual);

                foreach (var u in t.resource_groups)
                {
                    bw.Write(u.unknown_1);
                    bw.Write(u.id);
                    bw.Write(u.respawn);
                    bw.Write(u.amount);
                    bw.Write(u.unknown_2);
                }

                if (t.unknown_mutual <= 0) continue;

                for (var k = 0; k < t.unknown_mutual; k++)
                    bw.Write(t.mutualExclusionGroup[k]);
            }
        }

        private void JDSaveDynamics(BinaryWriter bw)
        {
            foreach (var t in jdnpcgen.dynamics)
            {
                bw.Write(t.id);
                bw.Write(t.spawn_x);
                bw.Write(t.spawn_alt);
                bw.Write(t.spawn_z);
                bw.Write(t.unknown_5);
                bw.Write(t.unknown_6);
                bw.Write(t.unknown_7);
                bw.Write(t.unknown_8);
                bw.Write(t.trigger);
            }
        }

        private void JDSaveTriggers(BinaryWriter bw)
        {
            foreach (var t in jdnpcgen.triggers)
            {
                bw.Write(t.unknown_1);
                bw.Write(t.unknown_2);
                bw.Write(t.name);
                bw.Write(t.unknown_3);
                bw.Write(t.unknown_4);
                bw.Write(t.unknown_5);
                bw.Write(t.unknown_6);
                bw.Write(t.unknown_7);
                bw.Write(t.year_1);
                bw.Write(t.month_1);
                bw.Write(t.week_day_1);
                bw.Write(t.day_1);
                bw.Write(t.hour_1);
                bw.Write(t.minute_1);
                bw.Write(t.year_2);
                bw.Write(t.month_2);
                bw.Write(t.week_day_2);
                bw.Write(t.day_2);
                bw.Write(t.hour_2);
                bw.Write(t.minute_2);
                bw.Write(t.duration);
                bw.Write(t.forced_stop);
            }
        }

        private void JDChangeCreatures(object sender, DataGridViewCellEventArgs e)
        {
            if (jdnpcgen == null || dataGridView_JDCreatures.CurrentCell == null) return;

            try
            {
                var r = dataGridView_JDCreatures.CurrentCell.RowIndex;

                switch (dataGridView_JDCreatures.CurrentCell.ColumnIndex)
                {
                    case 1:
                        jdnpcgen.creature_sets[r].spawn_mode = Column1.Items.IndexOf(dataGridView_JDCreatures.CurrentCell.Value);
                        break;

                    case 2:
                        jdnpcgen.creature_sets[r].spawn_x = Convert.ToSingle(dataGridView_JDCreatures.CurrentCell.Value);
                        break;

                    case 3:
                        jdnpcgen.creature_sets[r].spawn_alt = Convert.ToSingle(dataGridView_JDCreatures.CurrentCell.Value);
                        break;

                    case 4:
                        jdnpcgen.creature_sets[r].spawn_z = Convert.ToSingle(dataGridView_JDCreatures.CurrentCell.Value);
                        break;

                    case 5:
                        jdnpcgen.creature_sets[r].rot_1 = Convert.ToSingle(dataGridView_JDCreatures.CurrentCell.Value);
                        break;

                    case 6:
                        jdnpcgen.creature_sets[r].rot_2 = Convert.ToSingle(dataGridView_JDCreatures.CurrentCell.Value);
                        break;

                    case 7:
                        jdnpcgen.creature_sets[r].rot_3 = Convert.ToSingle(dataGridView_JDCreatures.CurrentCell.Value);
                        break;

                    case 8:
                        jdnpcgen.creature_sets[r].spread_x = Convert.ToSingle(dataGridView_JDCreatures.CurrentCell.Value);
                        break;

                    case 9:
                        jdnpcgen.creature_sets[r].spread_alt = Convert.ToSingle(dataGridView_JDCreatures.CurrentCell.Value);
                        break;

                    case 10:
                        jdnpcgen.creature_sets[r].spread_z = Convert.ToSingle(dataGridView_JDCreatures.CurrentCell.Value);
                        break;

                    case 11:
                        jdnpcgen.creature_sets[r].unknown_7 = Column12.Items.IndexOf(dataGridView_JDCreatures.CurrentCell.Value);
                        break;

                    case 12:
                        jdnpcgen.creature_sets[r].unknown_8 = Convert.ToInt32(dataGridView_JDCreatures.CurrentCell.Value);
                        break;

                    case 13:
                        jdnpcgen.creature_sets[r].unknown_9 = Convert.ToBoolean(dataGridView_JDCreatures.CurrentCell.Value);
                        break;

                    case 14:
                        jdnpcgen.creature_sets[r].unknown_10 = Convert.ToBoolean(dataGridView_JDCreatures.CurrentCell.Value);
                        break;

                    case 15:
                        jdnpcgen.creature_sets[r].unknown_11 = Convert.ToBoolean(dataGridView_JDCreatures.CurrentCell.Value);
                        break;

                    case 16:
                        jdnpcgen.creature_sets[r].unknown_12 = Convert.ToInt32(dataGridView_JDCreatures.CurrentCell.Value);
                        break;

                    case 17:
                        jdnpcgen.creature_sets[r].trigger = Convert.ToInt32(dataGridView_JDCreatures.CurrentCell.Value);
                        break;

                    case 18:
                        jdnpcgen.creature_sets[r].unknown_14 = Convert.ToInt32(dataGridView_JDCreatures.CurrentCell.Value);
                        break;

                    case 19:
                        jdnpcgen.creature_sets[r].unknown_15 = Convert.ToInt32(dataGridView_JDCreatures.CurrentCell.Value);
                        break;

                    case 20:
                        jdnpcgen.creature_sets[r].unknown_16 = Convert.ToInt32(dataGridView_JDCreatures.CurrentCell.Value);
                        break;

                    case 21:
                        jdnpcgen.creature_sets[r].unknown_17 = Convert.ToInt32(dataGridView_JDCreatures.CurrentCell.Value);
                        break;
                }
            }
            catch
            {
                MessageBox.Show(Resources.NPCFormatErr);
            }
        }

        private void JDChangeCreatureGroups(object sender, DataGridViewCellEventArgs e)
        {
            if (jdnpcgen == null || dataGridView_JDCreatures.CurrentCell == null ||
                dataGridView_JDCreatureGroups.CurrentCell == null) return;

            try
            {
                var r = dataGridView_JDCreatures.CurrentCell.RowIndex;
                var n = dataGridView_JDCreatureGroups.CurrentCell.RowIndex;

                switch (dataGridView_JDCreatureGroups.CurrentCell.ColumnIndex)
                {
                    case 0:
                        jdnpcgen.creature_sets[r].creature_groups[n].id = Convert.ToInt32(dataGridView_JDCreatureGroups.CurrentCell.Value);
                        if (n == 0)
                        {
                            dataGridView_JDCreatures.Rows[r].HeaderCell.Value =
                                "[" + jdnpcgen.creature_sets[r].creature_groups[0].id.ToString(CultureInfo.InvariantCulture) +
                                "] " + JDNPCName(jdnpcgen.creature_sets[r].creature_groups[0].id);
                        }
                        dataGridView_JDCreatureGroups.Rows[n].HeaderCell.Value = JDNPCName(jdnpcgen.creature_sets[r].creature_groups[n].id);
                        break;

                    case 1:
                        jdnpcgen.creature_sets[r].creature_groups[n].amount = Convert.ToInt32(dataGridView_JDCreatureGroups.CurrentCell.Value);
                        break;

                    case 2:
                        jdnpcgen.creature_sets[r].creature_groups[n].respawn = Convert.ToInt32(dataGridView_JDCreatureGroups.CurrentCell.Value);
                        break;

                    case 3:
                        jdnpcgen.creature_sets[r].creature_groups[n].unknown_2 = Convert.ToInt32(dataGridView_JDCreatureGroups.CurrentCell.Value);
                        break;

                    case 4:
                        jdnpcgen.creature_sets[r].creature_groups[n].unknown_3 = Convert.ToInt32(dataGridView_JDCreatureGroups.CurrentCell.Value);
                        break;

                    case 5:
                        jdnpcgen.creature_sets[r].creature_groups[n].unknown_4 = Convert.ToInt32(dataGridView_JDCreatureGroups.CurrentCell.Value);
                        break;

                    case 6:
                        jdnpcgen.creature_sets[r].creature_groups[n].unknown_5 = Convert.ToInt32(dataGridView_JDCreatureGroups.CurrentCell.Value);
                        break;

                    case 7:
                        jdnpcgen.creature_sets[r].creature_groups[n].unknown_6 = Convert.ToInt32(dataGridView_JDCreatureGroups.CurrentCell.Value);
                        break;

                    case 8:
                        jdnpcgen.creature_sets[r].creature_groups[n].unknown_7 = Convert.ToInt32(dataGridView_JDCreatureGroups.CurrentCell.Value);
                        break;

                    case 9:
                        jdnpcgen.creature_sets[r].creature_groups[n].unknown_8 = Convert.ToInt32(dataGridView_JDCreatureGroups.CurrentCell.Value);
                        break;

                    case 10:
                        jdnpcgen.creature_sets[r].creature_groups[n].unknown_9 = Convert.ToBoolean(dataGridView_JDCreatureGroups.CurrentCell.Value);
                        break;

                    case 11:
                        jdnpcgen.creature_sets[r].creature_groups[n].unknown_10 = Convert.ToBoolean(dataGridView_JDCreatureGroups.CurrentCell.Value);
                        break;

                    case 12:
                        jdnpcgen.creature_sets[r].creature_groups[n].unknown_11 = Convert.ToBoolean(dataGridView_JDCreatureGroups.CurrentCell.Value);
                        break;

                    case 13:
                        jdnpcgen.creature_sets[r].creature_groups[n].unknown_12 = Convert.ToBoolean(dataGridView_JDCreatureGroups.CurrentCell.Value);
                        break;

                    case 14:
                        jdnpcgen.creature_sets[r].creature_groups[n].unknown_13 = Convert.ToInt32(dataGridView_JDCreatureGroups.CurrentCell.Value);
                        break;

                    case 15:
                        jdnpcgen.creature_sets[r].creature_groups[n].unknown_14 = Convert.ToInt32(dataGridView_JDCreatureGroups.CurrentCell.Value);
                        break;

                    case 16:
                        jdnpcgen.creature_sets[r].creature_groups[n].unknown_15 = Convert.ToInt32(dataGridView_JDCreatureGroups.CurrentCell.Value);
                        break;

                    case 17:
                        jdnpcgen.creature_sets[r].creature_groups[n].unknown_16 = Convert.ToInt32(dataGridView_JDCreatureGroups.CurrentCell.Value);
                        break;
                }
            }
            catch
            {
                MessageBox.Show(Resources.NPCFormatErr);
            }
        }

        private void JDClickRowCreature(object sender, DataGridViewCellEventArgs e)
        {
            dataGridView_JDCreatureGroups.Rows.Clear();
            dataGridView_JDCreatureGroups.ContextMenuStrip = null;

            if (jdnpcgen == null || e.RowIndex <= -1 || e.RowIndex >= jdnpcgen.creature_sets.Length) return;

            for (var i = 0; i < jdnpcgen.creature_sets[e.RowIndex].creature_groups_count; i++)
            {
                var array = new object[18];
                var id = jdnpcgen.creature_sets[e.RowIndex].creature_groups[i].id;
                array[0] = id.ToString(CultureInfo.InvariantCulture);

                var amount = jdnpcgen.creature_sets[e.RowIndex].creature_groups[i].amount;
                array[1] = amount.ToString(CultureInfo.InvariantCulture);

                var respawn = jdnpcgen.creature_sets[e.RowIndex].creature_groups[i].respawn;
                array[2] = respawn.ToString(CultureInfo.InvariantCulture);

                var unknown = jdnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_2;
                array[3] = unknown.ToString(CultureInfo.InvariantCulture);

                var unknown2 = jdnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_3;
                array[4] = unknown2.ToString(CultureInfo.InvariantCulture);

                var unknown3 = jdnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_4;
                array[5] = unknown3.ToString(CultureInfo.InvariantCulture);

                var unknown4 = jdnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_5;
                array[6] = unknown4.ToString(CultureInfo.InvariantCulture);

                var unknown5 = jdnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_6;
                array[7] = unknown5.ToString(CultureInfo.InvariantCulture);

                var unknown6 = jdnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_7;
                array[8] = unknown6.ToString(CultureInfo.InvariantCulture);

                var unknown7 = jdnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_8;
                array[9] = unknown7.ToString(CultureInfo.InvariantCulture);

                var unknown8 = jdnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_9;
                array[10] = unknown8.ToString(CultureInfo.InvariantCulture);

                var unknown9 = jdnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_10;
                array[11] = unknown9.ToString(CultureInfo.InvariantCulture);

                var unknown10 = jdnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_11;
                array[12] = unknown10.ToString(CultureInfo.InvariantCulture);

                var unknown11 = jdnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_12;
                array[13] = unknown11.ToString(CultureInfo.InvariantCulture);

                var unknown12 = jdnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_13;
                array[14] = unknown12.ToString(CultureInfo.InvariantCulture);

                var unknown13 = jdnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_14;
                array[15] = unknown13.ToString(CultureInfo.InvariantCulture);

                var unknown14 = jdnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_15;
                array[16] = unknown14.ToString(CultureInfo.InvariantCulture);

                var unknown15 = jdnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_16;
                array[17] = unknown15.ToString(CultureInfo.InvariantCulture);

                dataGridView_JDCreatureGroups.Rows.Add(array);
                dataGridView_JDCreatureGroups.CellValueChanged -= JDChangeCreatureGroups;
                dataGridView_JDCreatureGroups.Rows[i].HeaderCell.Value = JDNPCName(jdnpcgen.creature_sets[e.RowIndex].creature_groups[i].id);
                dataGridView_JDCreatureGroups.CellValueChanged += JDChangeCreatureGroups;
            }

            dataGridView_JDCreatureGroups.ContextMenuStrip = contextJDDataGrid;
        }

        private void JDChangeResources(object sender, DataGridViewCellEventArgs e)
        {
            if (jdnpcgen == null || dataGridView_JDResources.CurrentCell == null) return;

            try
            {
                var r = dataGridView_JDResources.CurrentCell.RowIndex;

                switch (dataGridView_JDResources.CurrentCell.ColumnIndex)
                {
                    case 1:
                        jdnpcgen.resource_sets[r].spawn_x = Convert.ToSingle(dataGridView_JDResources.CurrentCell.Value);
                        break;

                    case 2:
                        jdnpcgen.resource_sets[r].spawn_alt = Convert.ToSingle(dataGridView_JDResources.CurrentCell.Value);
                        break;

                    case 3:
                        jdnpcgen.resource_sets[r].spawn_z = Convert.ToSingle(dataGridView_JDResources.CurrentCell.Value);
                        break;

                    case 4:
                        jdnpcgen.resource_sets[r].spread_x = Convert.ToSingle(dataGridView_JDResources.CurrentCell.Value);
                        break;

                    case 5:
                        jdnpcgen.resource_sets[r].spread_z = Convert.ToSingle(dataGridView_JDResources.CurrentCell.Value);
                        break;

                    case 6:
                        jdnpcgen.resource_sets[r].unknown_1 = Convert.ToBoolean(dataGridView_JDResources.CurrentCell.Value);
                        break;

                    case 7:
                        jdnpcgen.resource_sets[r].unknown_2 = Convert.ToBoolean(dataGridView_JDResources.CurrentCell.Value);
                        break;

                    case 8:
                        jdnpcgen.resource_sets[r].unknown_3 = Convert.ToBoolean(dataGridView_JDResources.CurrentCell.Value);
                        break;

                    case 9:
                        jdnpcgen.resource_sets[r].unknown_4 = Convert.ToInt32(dataGridView_JDResources.CurrentCell.Value);
                        break;

                    case 10:
                        jdnpcgen.resource_sets[r].unknown_5 = Convert.ToByte(dataGridView_JDResources.CurrentCell.Value);
                        break;

                    case 11:
                        jdnpcgen.resource_sets[r].unknown_6 = Convert.ToByte(dataGridView_JDResources.CurrentCell.Value);
                        break;

                    case 12:
                        jdnpcgen.resource_sets[r].unknown_7 = Convert.ToByte(dataGridView_JDResources.CurrentCell.Value);
                        break;

                    case 13:
                        jdnpcgen.resource_sets[r].unknown_trigger = Convert.ToInt32(dataGridView_JDResources.CurrentCell.Value);
                        break;

                    case 14:
                        jdnpcgen.resource_sets[r].unknown_8 = Convert.ToInt32(dataGridView_JDResources.CurrentCell.Value);
                        break;

                    case 15:
                        jdnpcgen.resource_sets[r].unknown_index = Convert.ToInt32(dataGridView_JDResources.CurrentCell.Value);
                        break;

                    case 16:
                        jdnpcgen.resource_sets[r].unknown_mutual = Convert.ToInt32(dataGridView_JDResources.CurrentCell.Value);
                        break;
                }
            }
            catch (Exception)
            {
                MessageBox.Show(Resources.NPCFormatErr);
            }
        }

        private void JDClickRowResource(object sender, DataGridViewCellEventArgs e)
        {
            dataGridView_JDResourceGroups.Rows.Clear();
            dataGridView_JDResourceGroups.ContextMenuStrip = null;

            if (jdnpcgen == null || e.RowIndex <= -1 || e.RowIndex >= jdnpcgen.resource_sets.Length) return;

            for (var i = 0; i < jdnpcgen.resource_sets[e.RowIndex].resource_groups_count; i++)
            {
                var array = new object[5];
                var unknown = jdnpcgen.resource_sets[e.RowIndex].resource_groups[i].unknown_1;
                array[0] = unknown.ToString(CultureInfo.InvariantCulture);

                var id = jdnpcgen.resource_sets[e.RowIndex].resource_groups[i].id;
                array[1] = id.ToString(CultureInfo.InvariantCulture);

                var respawn = jdnpcgen.resource_sets[e.RowIndex].resource_groups[i].respawn;
                array[2] = respawn.ToString(CultureInfo.InvariantCulture);

                var amount = jdnpcgen.resource_sets[e.RowIndex].resource_groups[i].amount;
                array[3] = amount.ToString(CultureInfo.InvariantCulture);

                var unknown2 = jdnpcgen.resource_sets[e.RowIndex].resource_groups[i].unknown_2;
                array[4] = unknown2.ToString(CultureInfo.InvariantCulture);

                dataGridView_JDResourceGroups.Rows.Add(array);
            }
            dataGridView_JDResourceGroups.ContextMenuStrip = contextJDDataGrid;
        }

        private void JDChangeResourceGroups(object sender, DataGridViewCellEventArgs e)
        {
            if (jdnpcgen == null || dataGridView_JDResources.CurrentCell == null ||
                dataGridView_JDResourceGroups.CurrentCell == null) return;

            try
            {
                var r = dataGridView_JDResources.CurrentCell.RowIndex;
                var n = dataGridView_JDResourceGroups.CurrentCell.RowIndex;

                switch (dataGridView_JDResourceGroups.CurrentCell.ColumnIndex)
                {
                    case 0:
                        jdnpcgen.resource_sets[r].resource_groups[n].unknown_1 = Convert.ToInt32(dataGridView_JDResourceGroups.CurrentCell.Value);
                        break;

                    case 1:
                        jdnpcgen.resource_sets[r].resource_groups[n].id = Convert.ToInt32(dataGridView_JDResourceGroups.CurrentCell.Value);

                        if (n == 0)
                        {
                            var id = jdnpcgen.resource_sets[r].resource_groups[0].id;
                            dataGridView_JDResources.Rows[r].HeaderCell.Value = id.ToString(CultureInfo.InvariantCulture);
                        }
                        break;

                    case 2:
                        jdnpcgen.resource_sets[r].resource_groups[n].respawn = Convert.ToInt32(dataGridView_JDResourceGroups.CurrentCell.Value);
                        break;

                    case 3:
                        jdnpcgen.resource_sets[r].resource_groups[n].amount = Convert.ToInt32(dataGridView_JDResourceGroups.CurrentCell.Value);
                        break;

                    case 4:
                        jdnpcgen.resource_sets[r].resource_groups[n].unknown_2 = Convert.ToInt32(dataGridView_JDResourceGroups.CurrentCell.Value);
                        break;
                }
            }
            catch (Exception)
            {
                MessageBox.Show(Resources.NPCFormatErr);
            }
        }

        private void JDChangeDynamics(object sender, DataGridViewCellEventArgs e)
        {
            if (jdnpcgen == null || dataGridView_JDDynamics.CurrentCell == null) return;

            try
            {
                var r = dataGridView_JDDynamics.CurrentCell.RowIndex;

                switch (dataGridView_JDDynamics.CurrentCell.ColumnIndex)
                {
                    case 0:
                        jdnpcgen.dynamics[r].id = Convert.ToInt32(dataGridView_JDDynamics.CurrentCell.Value);
                        break;

                    case 1:
                        jdnpcgen.dynamics[r].spawn_x = Convert.ToSingle(dataGridView_JDDynamics.CurrentCell.Value);
                        break;

                    case 2:
                        jdnpcgen.dynamics[r].spawn_alt = Convert.ToSingle(dataGridView_JDDynamics.CurrentCell.Value);
                        break;

                    case 3:
                        jdnpcgen.dynamics[r].spawn_z = Convert.ToSingle(dataGridView_JDDynamics.CurrentCell.Value);
                        break;

                    case 4:
                        jdnpcgen.dynamics[r].unknown_5 = Convert.ToByte(dataGridView_JDDynamics.CurrentCell.Value);
                        break;

                    case 5:
                        jdnpcgen.dynamics[r].unknown_6 = Convert.ToByte(dataGridView_JDDynamics.CurrentCell.Value);
                        break;

                    case 6:
                        jdnpcgen.dynamics[r].unknown_7 = Convert.ToByte(dataGridView_JDDynamics.CurrentCell.Value);
                        break;

                    case 7:
                        jdnpcgen.dynamics[r].unknown_8 = Convert.ToByte(dataGridView_JDDynamics.CurrentCell.Value);
                        break;

                    case 8:
                        jdnpcgen.dynamics[r].trigger = Convert.ToInt32(dataGridView_JDDynamics.CurrentCell.Value);
                        break;
                }
            }
            catch (Exception)
            {
                MessageBox.Show(Resources.NPCFormatErr);
            }
        }

        private void JDChangeTriggers(object sender, DataGridViewCellEventArgs e)
        {
            if (jdnpcgen == null || dataGridView_JDTriggers.CurrentCell == null) return;

            try
            {
                var r = dataGridView_JDTriggers.CurrentCell.RowIndex;

                switch (dataGridView_JDTriggers.CurrentCell.ColumnIndex)
                {
                    case 0:
                        jdnpcgen.triggers[r].unknown_1 = Convert.ToInt32(dataGridView_JDTriggers.CurrentCell.Value);
                        break;

                    case 1:
                        jdnpcgen.triggers[r].unknown_2 = Convert.ToInt32(dataGridView_JDTriggers.CurrentCell.Value);
                        break;

                    case 2:
                        jdnpcgen.triggers[r].name = Common.GetBytesGBK(dataGridView_JDTriggers.CurrentCell.Value.ToString());
                        break;

                    case 3:
                        jdnpcgen.triggers[r].unknown_3 = Convert.ToBoolean(dataGridView_JDTriggers.CurrentCell.Value);
                        break;

                    case 4:
                        jdnpcgen.triggers[r].unknown_4 = Convert.ToInt32(dataGridView_JDTriggers.CurrentCell.Value);
                        break;

                    case 5:
                        jdnpcgen.triggers[r].unknown_5 = Convert.ToInt32(dataGridView_JDTriggers.CurrentCell.Value);
                        break;

                    case 6:
                        jdnpcgen.triggers[r].unknown_6 = Convert.ToBoolean(dataGridView_JDTriggers.CurrentCell.Value);
                        break;

                    case 7:
                        jdnpcgen.triggers[r].unknown_7 = Convert.ToBoolean(dataGridView_JDTriggers.CurrentCell.Value);
                        break;

                    case 8:
                        jdnpcgen.triggers[r].year_1 = Convert.ToInt32(dataGridView_JDTriggers.CurrentCell.Value);
                        break;

                    case 9:
                        jdnpcgen.triggers[r].month_1 = Convert.ToInt32(dataGridView_JDTriggers.CurrentCell.Value);
                        break;

                    case 10:
                        jdnpcgen.triggers[r].week_day_1 = Column42.Items.IndexOf(dataGridView_JDTriggers.CurrentCell.Value) - 1;
                        break;

                    case 11:
                        jdnpcgen.triggers[r].day_1 = Convert.ToInt32(dataGridView_JDTriggers.CurrentCell.Value);
                        break;

                    case 12:
                        jdnpcgen.triggers[r].hour_1 = Convert.ToInt32(dataGridView_JDTriggers.CurrentCell.Value);
                        break;

                    case 13:
                        jdnpcgen.triggers[r].minute_1 = Convert.ToInt32(dataGridView_JDTriggers.CurrentCell.Value);
                        break;

                    case 14:
                        jdnpcgen.triggers[r].year_2 = Convert.ToInt32(dataGridView_JDTriggers.CurrentCell.Value);
                        break;

                    case 15:
                        jdnpcgen.triggers[r].month_2 = Convert.ToInt32(dataGridView_JDTriggers.CurrentCell.Value);
                        break;

                    case 16:
                        jdnpcgen.triggers[r].week_day_2 = Column43.Items.IndexOf(dataGridView_JDTriggers.CurrentCell.Value) - 1;
                        break;

                    case 17:
                        jdnpcgen.triggers[r].day_2 = Convert.ToInt32(dataGridView_JDTriggers.CurrentCell.Value);
                        break;

                    case 18:
                        jdnpcgen.triggers[r].hour_2 = Convert.ToInt32(dataGridView_JDTriggers.CurrentCell.Value);
                        break;

                    case 19:
                        jdnpcgen.triggers[r].minute_2 = Convert.ToInt32(dataGridView_JDTriggers.CurrentCell.Value);
                        break;

                    case 20:
                        jdnpcgen.triggers[r].duration = Convert.ToInt32(dataGridView_JDTriggers.CurrentCell.Value);
                        break;

                    case 21:
                        jdnpcgen.triggers[r].forced_stop = Convert.ToBoolean(dataGridView_JDTriggers.CurrentCell.Value);
                        break;
                }
            }
            catch (Exception)
            {
                MessageBox.Show(Resources.NPCFormatErr);
            }
        }

        private void JDAddRowClick(object sender, EventArgs e)
        {
            if (jdnpcgen == null) return;

            if (contextJDDataGrid.SourceControl == dataGridView_JDCreatures)
            {
                jdnpcgen.creature_sets_count = jdnpcgen.creature_sets_count + 1;
                Array.Resize(ref jdnpcgen.creature_sets, jdnpcgen.creature_sets.Length + 1);
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1] = new JDCreatureSets();

                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].spawn_mode = 0;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].creature_groups_count = 0;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].spawn_x = 0f;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].spawn_alt = 0f;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].spawn_z = 0f;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].rot_1 = 0f;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].rot_2 = 0f;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].rot_3 = 0f;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].spread_x = 0f;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].spread_alt = 0f;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].spread_z = 0f;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].unknown_7 = 0;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].unknown_8 = 0;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].unknown_9 = true;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].unknown_10 = true;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].unknown_11 = true;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].unknown_12 = 0;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].trigger = 0;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].unknown_14 = 0;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].unknown_15 = 0;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].unknown_16 = 0;
                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].unknown_17 = 0;

                jdnpcgen.creature_sets[jdnpcgen.creature_sets.Length - 1].creature_groups = new JDCreatureGroup[0];

                dataGridView_JDCreatures.Rows.Add("0", Column1.Items[0].ToString(), "0", "0", "0", "0", "0", "0", "0", "0", "0",
                    Column12.Items[0].ToString(), "0", "True", "True", "True", "0", "0", "0", "0", "0", "0");
                dataGridView_JDCreatures.Rows[dataGridView_JDCreatures.Rows.Count - 1].HeaderCell.Value = "";
            }

            if (contextJDDataGrid.SourceControl == dataGridView_JDCreatureGroups)
            {
                var r = dataGridView_JDCreatures.CurrentCell.RowIndex;

                jdnpcgen.creature_sets[r].creature_groups_count = jdnpcgen.creature_sets[r].creature_groups_count + 1;
                Array.Resize(ref jdnpcgen.creature_sets[r].creature_groups, jdnpcgen.creature_sets[r].creature_groups.Length + 1);
                jdnpcgen.creature_sets[r].creature_groups[jdnpcgen.creature_sets[r].creature_groups.Length - 1] = new JDCreatureGroup();

                jdnpcgen.creature_sets[r].creature_groups[jdnpcgen.creature_sets[r].creature_groups.Length - 1].id = 0;
                jdnpcgen.creature_sets[r].creature_groups[jdnpcgen.creature_sets[r].creature_groups.Length - 1].amount = 0;
                jdnpcgen.creature_sets[r].creature_groups[jdnpcgen.creature_sets[r].creature_groups.Length - 1].respawn = 0;
                jdnpcgen.creature_sets[r].creature_groups[jdnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_2 = 0;
                jdnpcgen.creature_sets[r].creature_groups[jdnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_3 = 0;
                jdnpcgen.creature_sets[r].creature_groups[jdnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_4 = 0;
                jdnpcgen.creature_sets[r].creature_groups[jdnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_5 = 0;
                jdnpcgen.creature_sets[r].creature_groups[jdnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_6 = 0;
                jdnpcgen.creature_sets[r].creature_groups[jdnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_7 = 0;
                jdnpcgen.creature_sets[r].creature_groups[jdnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_8 = 0;
                jdnpcgen.creature_sets[r].creature_groups[jdnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_9 = false;
                jdnpcgen.creature_sets[r].creature_groups[jdnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_10 = true;
                jdnpcgen.creature_sets[r].creature_groups[jdnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_11 = true;
                jdnpcgen.creature_sets[r].creature_groups[jdnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_12 = true;
                jdnpcgen.creature_sets[r].creature_groups[jdnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_13 = 0;
                jdnpcgen.creature_sets[r].creature_groups[jdnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_14 = 0;
                jdnpcgen.creature_sets[r].creature_groups[jdnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_15 = 0;
                jdnpcgen.creature_sets[r].creature_groups[jdnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_16 = 0;

                dataGridView_JDCreatureGroups.Rows.Add("0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "False",
                    "True", "True", "True", "0", "0", "0", "0");

                if (jdnpcgen.creature_sets[r].creature_groups.Length > 0)
                {
                    var id = jdnpcgen.creature_sets[r].creature_groups[0].id;
                    dataGridView_JDCreatures.Rows[r].HeaderCell.Value = "[" + id.ToString(CultureInfo.InvariantCulture) + "] " +
                        JDNPCName(jdnpcgen.creature_sets[r].creature_groups[0].id);
                }
                else
                {
                    dataGridView_JDCreatures.Rows[r].HeaderCell.Value = "";
                }

                dataGridView_JDCreatures.Rows[r].Cells[0].Value =
                    dataGridView_JDCreatureGroups.Rows.Count.ToString(CultureInfo.InvariantCulture);
            }

            if (contextJDDataGrid.SourceControl == dataGridView_JDResources)
            {
                jdnpcgen.resource_sets_count = jdnpcgen.resource_sets_count + 1;
                Array.Resize(ref jdnpcgen.resource_sets, jdnpcgen.resource_sets.Length + 1);
                jdnpcgen.resource_sets[jdnpcgen.resource_sets.Length - 1] = new JDResourceSets();

                jdnpcgen.resource_sets[jdnpcgen.resource_sets.Length - 1].spawn_x = 0f;
                jdnpcgen.resource_sets[jdnpcgen.resource_sets.Length - 1].spawn_alt = 0f;
                jdnpcgen.resource_sets[jdnpcgen.resource_sets.Length - 1].spawn_z = 0f;
                jdnpcgen.resource_sets[jdnpcgen.resource_sets.Length - 1].spread_x = 0f;
                jdnpcgen.resource_sets[jdnpcgen.resource_sets.Length - 1].spread_z = 0f;
                jdnpcgen.resource_sets[jdnpcgen.resource_sets.Length - 1].resource_groups_count = 0;
                jdnpcgen.resource_sets[jdnpcgen.resource_sets.Length - 1].unknown_1 = true;
                jdnpcgen.resource_sets[jdnpcgen.resource_sets.Length - 1].unknown_2 = true;
                jdnpcgen.resource_sets[jdnpcgen.resource_sets.Length - 1].unknown_3 = true;
                jdnpcgen.resource_sets[jdnpcgen.resource_sets.Length - 1].unknown_4 = 0;
                jdnpcgen.resource_sets[jdnpcgen.resource_sets.Length - 1].unknown_5 = 192;
                jdnpcgen.resource_sets[jdnpcgen.resource_sets.Length - 1].unknown_6 = 63;
                jdnpcgen.resource_sets[jdnpcgen.resource_sets.Length - 1].unknown_7 = 0;
                jdnpcgen.resource_sets[jdnpcgen.resource_sets.Length - 1].unknown_trigger = 0;
                jdnpcgen.resource_sets[jdnpcgen.resource_sets.Length - 1].unknown_8 = 0;
                jdnpcgen.resource_sets[jdnpcgen.resource_sets.Length - 1].unknown_index = 0;
                jdnpcgen.resource_sets[jdnpcgen.resource_sets.Length - 1].unknown_mutual = 0;

                jdnpcgen.resource_sets[jdnpcgen.resource_sets.Length - 1].resource_groups = new JDResourceGroup[0];

                dataGridView_JDResources.Rows.Add("0", "0", "0", "0", "0", "0", "True",
                    "True", "True", "0", "192", "63", "0", "0", "0", "0", "0");
                dataGridView_JDResources.Rows[dataGridView_JDResources.Rows.Count - 1].HeaderCell.Value = "";
            }

            if (contextJDDataGrid.SourceControl == dataGridView_JDResourceGroups)
            {
                var i = dataGridView_JDResources.CurrentCell.RowIndex;

                jdnpcgen.resource_sets[i].resource_groups_count = jdnpcgen.resource_sets[i].resource_groups_count + 1;
                Array.Resize(ref jdnpcgen.resource_sets[i].resource_groups, jdnpcgen.resource_sets[i].resource_groups.Length + 1);
                jdnpcgen.resource_sets[i].resource_groups[jdnpcgen.resource_sets[i].resource_groups.Length - 1] = new JDResourceGroup();

                jdnpcgen.resource_sets[i].resource_groups[jdnpcgen.resource_sets[i].resource_groups.Length - 1].unknown_1 = 0;
                jdnpcgen.resource_sets[i].resource_groups[jdnpcgen.resource_sets[i].resource_groups.Length - 1].id = 0;
                jdnpcgen.resource_sets[i].resource_groups[jdnpcgen.resource_sets[i].resource_groups.Length - 1].respawn = 0;
                jdnpcgen.resource_sets[i].resource_groups[jdnpcgen.resource_sets[i].resource_groups.Length - 1].amount = 0;
                jdnpcgen.resource_sets[i].resource_groups[jdnpcgen.resource_sets[i].resource_groups.Length - 1].unknown_2 = 0;

                dataGridView_JDResourceGroups.Rows.Add("0", "0", "0", "0", "0");

                dataGridView_JDResources.Rows[i].HeaderCell.Value =
                    jdnpcgen.resource_sets[i].resource_groups.Length > 0 ?
                    jdnpcgen.resource_sets[i].resource_groups[0].id.ToString(CultureInfo.InvariantCulture) : "";

                dataGridView_JDResources.Rows[i].Cells[0].Value =
                    (dataGridView_JDResourceGroups.Rows.Count).ToString(CultureInfo.InvariantCulture);
            }

            if (contextJDDataGrid.SourceControl == dataGridView_JDDynamics)
            {
                jdnpcgen.dynamics_count = jdnpcgen.dynamics_count + 1;
                Array.Resize(ref jdnpcgen.dynamics, jdnpcgen.dynamics.Length + 1);
                jdnpcgen.dynamics[jdnpcgen.dynamics.Length - 1] = new JDDynamic();

                jdnpcgen.dynamics[jdnpcgen.dynamics.Length - 1].id = 0;
                jdnpcgen.dynamics[jdnpcgen.dynamics.Length - 1].spawn_x = 0f;
                jdnpcgen.dynamics[jdnpcgen.dynamics.Length - 1].spawn_alt = 0f;
                jdnpcgen.dynamics[jdnpcgen.dynamics.Length - 1].spawn_z = 0f;
                jdnpcgen.dynamics[jdnpcgen.dynamics.Length - 1].unknown_5 = 0;
                jdnpcgen.dynamics[jdnpcgen.dynamics.Length - 1].unknown_6 = 0;
                jdnpcgen.dynamics[jdnpcgen.dynamics.Length - 1].unknown_7 = 0;
                jdnpcgen.dynamics[jdnpcgen.dynamics.Length - 1].unknown_8 = 16;
                jdnpcgen.dynamics[jdnpcgen.dynamics.Length - 1].trigger = 0;

                dataGridView_JDDynamics.Rows.Add("0", "0", "0", "0", "0", "0", "0", "16", "0");
                dataGridView_JDDynamics.Rows[dataGridView_JDDynamics.Rows.Count - 1].HeaderCell.Value =
                    (dataGridView_JDDynamics.Rows.Count - 1).ToString(CultureInfo.InvariantCulture);
            }

            if (contextJDDataGrid.SourceControl != dataGridView_JDTriggers) return;

            jdnpcgen.triggers_count = jdnpcgen.triggers_count + 1;
            Array.Resize(ref jdnpcgen.triggers, jdnpcgen.triggers.Length + 1);
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1] = new JDTrigger();

            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].unknown_1 = 0;
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].unknown_2 = 0;
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].name = new byte[128];
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].unknown_3 = false;
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].unknown_4 = 0;
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].unknown_5 = 0;
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].unknown_6 = false;
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].unknown_7 = false;
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].year_1 = -1;
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].month_1 = -1;
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].week_day_1 = -1;
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].day_1 = -1;
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].hour_1 = -1;
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].minute_1 = -1;
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].year_2 = -1;
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].month_2 = -1;
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].week_day_2 = -1;
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].day_2 = -1;
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].hour_2 = -1;
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].minute_2 = -1;
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].duration = 0;
            jdnpcgen.triggers[jdnpcgen.triggers.Length - 1].forced_stop = false;

            dataGridView_JDTriggers.Rows.Add("0", "0", "", "False", "0", "0", "True", "True", "-1", "-1",
                Column42.Items[0].ToString(), "-1", "-1", "0", "-1", "-1", Column43.Items[0].ToString(),
                "-1", "-1", "0", "60", "False");

            dataGridView_JDTriggers.Rows[dataGridView_JDTriggers.Rows.Count - 1].HeaderCell.Value =
                (dataGridView_JDTriggers.Rows.Count - 1).ToString(CultureInfo.InvariantCulture);
        }

        private void JDDeleteRowClick(object sender, EventArgs e)
        {
            if (jdnpcgen == null) return;

            if (contextJDDataGrid.SourceControl == dataGridView_JDCreatures)
            {
                if (jdnpcgen.creature_sets_count > 0 && dataGridView_JDCreatures.CurrentCell.RowIndex > -1)
                {
                    jdnpcgen.creature_sets_count--;

                    var tempCS = new JDCreatureSets[jdnpcgen.creature_sets_count];
                    Array.Copy(jdnpcgen.creature_sets, 0, tempCS, 0, dataGridView_JDCreatures.CurrentCell.RowIndex);
                    Array.Copy(jdnpcgen.creature_sets, dataGridView_JDCreatures.CurrentCell.RowIndex + 1, tempCS,
                        dataGridView_JDCreatures.CurrentCell.RowIndex, jdnpcgen.creature_sets_count - dataGridView_JDCreatures.CurrentCell.RowIndex);
                    jdnpcgen.creature_sets = tempCS;

                    dataGridView_JDCreatureGroups.Rows.Clear();
                    dataGridView_JDCreatures.Rows.RemoveAt(dataGridView_JDCreatures.CurrentCell.RowIndex);
                }
            }

            if (contextJDDataGrid.SourceControl == dataGridView_JDCreatureGroups)
            {
                if (dataGridView_JDCreatures.CurrentCell.RowIndex > -1)
                {
                    var r = dataGridView_JDCreatures.CurrentCell.RowIndex;

                    if (jdnpcgen.creature_sets[r].creature_groups_count > 0 && dataGridView_JDCreatureGroups.CurrentCell.RowIndex > -1)
                    {
                        jdnpcgen.creature_sets[r].creature_groups_count--;

                        var tempCG = new JDCreatureGroup[jdnpcgen.creature_sets[r].creature_groups_count];
                        Array.Copy(jdnpcgen.creature_sets[r].creature_groups, 0, tempCG, 0, dataGridView_JDCreatureGroups.CurrentCell.RowIndex);
                        Array.Copy(jdnpcgen.creature_sets[r].creature_groups, dataGridView_JDCreatureGroups.CurrentCell.RowIndex + 1, tempCG,
                            dataGridView_JDCreatureGroups.CurrentCell.RowIndex,
                            jdnpcgen.creature_sets[r].creature_groups_count - dataGridView_JDCreatureGroups.CurrentCell.RowIndex);
                        jdnpcgen.creature_sets[r].creature_groups = tempCG;

                        dataGridView_JDCreatureGroups.Rows.RemoveAt(dataGridView_JDCreatureGroups.CurrentCell.RowIndex);

                        if (jdnpcgen.creature_sets[r].creature_groups.Length > 0)
                        {
                            dataGridView_JDCreatures.Rows[r].HeaderCell.Value =
                                "[" + jdnpcgen.creature_sets[r].creature_groups[0].id.ToString(CultureInfo.InvariantCulture) + "] " +
                                JDNPCName(jdnpcgen.creature_sets[r].creature_groups[0].id);
                        }
                        else
                        {
                            dataGridView_JDCreatures.Rows[r].HeaderCell.Value = "";
                        }

                        dataGridView_JDCreatures.Rows[r].Cells[0].Value =
                            (dataGridView_JDCreatureGroups.Rows.Count).ToString(CultureInfo.InvariantCulture);
                    }
                }
            }

            if (contextJDDataGrid.SourceControl == dataGridView_JDResources)
            {
                if (jdnpcgen.resource_sets_count > 0 && dataGridView_JDResources.CurrentCell.RowIndex > -1)
                {
                    jdnpcgen.resource_sets_count--;

                    var tempRS = new JDResourceSets[jdnpcgen.resource_sets_count];
                    Array.Copy(jdnpcgen.resource_sets, 0, tempRS, 0, dataGridView_JDResources.CurrentCell.RowIndex);
                    Array.Copy(jdnpcgen.resource_sets, dataGridView_JDResources.CurrentCell.RowIndex + 1, tempRS,
                        dataGridView_JDResources.CurrentCell.RowIndex, jdnpcgen.resource_sets_count - dataGridView_JDResources.CurrentCell.RowIndex);
                    jdnpcgen.resource_sets = tempRS;

                    dataGridView_JDResourceGroups.Rows.Clear();
                    dataGridView_JDResources.Rows.RemoveAt(dataGridView_JDResources.CurrentCell.RowIndex);
                }
            }

            if (contextJDDataGrid.SourceControl == dataGridView_JDResourceGroups)
            {
                if (dataGridView_JDResources.CurrentCell.RowIndex > -1)
                {
                    var r = dataGridView_JDResources.CurrentCell.RowIndex;

                    if (jdnpcgen.resource_sets[r].resource_groups_count > 0 && dataGridView_JDResourceGroups.CurrentCell.RowIndex > -1)
                    {
                        jdnpcgen.resource_sets[r].resource_groups_count--;

                        var tempRG = new JDResourceGroup[jdnpcgen.resource_sets[r].resource_groups_count];
                        Array.Copy(jdnpcgen.resource_sets[r].resource_groups, 0, tempRG, 0, dataGridView_JDResourceGroups.CurrentCell.RowIndex);
                        Array.Copy(jdnpcgen.resource_sets[r].resource_groups, dataGridView_JDResourceGroups.CurrentCell.RowIndex + 1, tempRG,
                            dataGridView_JDResourceGroups.CurrentCell.RowIndex,
                            jdnpcgen.resource_sets[r].resource_groups_count - dataGridView_JDResourceGroups.CurrentCell.RowIndex);
                        jdnpcgen.resource_sets[r].resource_groups = tempRG;

                        dataGridView_JDResourceGroups.Rows.RemoveAt(dataGridView_JDResourceGroups.CurrentCell.RowIndex);
                        dataGridView_JDResources.Rows[r].HeaderCell.Value = jdnpcgen.resource_sets[r].resource_groups.Length > 0 ?
                            jdnpcgen.resource_sets[r].resource_groups[0].id.ToString(CultureInfo.InvariantCulture) : "";
                        dataGridView_JDResources.Rows[r].Cells[0].Value = (dataGridView_JDResourceGroups.Rows.Count).ToString(CultureInfo.InvariantCulture);
                    }
                }
            }

            if (contextJDDataGrid.SourceControl == dataGridView_JDDynamics)
            {
                if (jdnpcgen.dynamics_count > 0 && dataGridView_JDDynamics.CurrentCell.RowIndex > -1)
                {
                    jdnpcgen.dynamics_count--;

                    var tempD = new JDDynamic[jdnpcgen.dynamics_count];
                    Array.Copy(jdnpcgen.dynamics, 0, tempD, 0, dataGridView_JDDynamics.CurrentCell.RowIndex);
                    Array.Copy(jdnpcgen.dynamics, dataGridView_JDDynamics.CurrentCell.RowIndex + 1, tempD,
                        dataGridView_JDDynamics.CurrentCell.RowIndex, jdnpcgen.dynamics_count - dataGridView_JDDynamics.CurrentCell.RowIndex);
                    jdnpcgen.dynamics = tempD;

                    dataGridView_JDDynamics.Rows.RemoveAt(dataGridView_JDDynamics.CurrentCell.RowIndex);
                }
            }

            if (contextJDDataGrid.SourceControl != dataGridView_JDTriggers) return;

            if (jdnpcgen.triggers_count <= 0 || dataGridView_JDTriggers.CurrentCell.RowIndex <= -1) return;
            jdnpcgen.triggers_count--;

            var tempT = new JDTrigger[jdnpcgen.triggers_count];
            Array.Copy(jdnpcgen.triggers, 0, tempT, 0, dataGridView_JDTriggers.CurrentCell.RowIndex);
            Array.Copy(jdnpcgen.triggers, dataGridView_JDTriggers.CurrentCell.RowIndex + 1, tempT,
                       dataGridView_JDTriggers.CurrentCell.RowIndex, jdnpcgen.triggers_count - dataGridView_JDTriggers.CurrentCell.RowIndex);
            jdnpcgen.triggers = tempT;

            dataGridView_JDTriggers.Rows.RemoveAt(dataGridView_JDTriggers.CurrentCell.RowIndex);
        }


    }
}
